﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using JQuery.WebKit.Base;
using JQuery.WebKit.WebCommon;

#region resources
[assembly: WebResource("JQuery.WebKit.JValueSelector.valueSelector.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JValueSelector.valueSelector.min.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JValueSelector.valueSelector.css", "text/css")]
#endregion
namespace JQuery.WebKit
{
    public class JValueSelector : JControl
    {
        //[JavaScript("onButtonClick", Category = "options")]
        public string OnButtonClick { get; set; }

        public string TooltipText { get; set; }

        internal override PostType PostType
        {
            get { return Base.PostType.TextAndValue; }
        }

        public Dictionary<object, object> Items;

        public JValueSelector()
            : base("div")
        {
            Items = new Dictionary<object, object>();
        }

        public JValueSelector(string tag)
            : base("div")
        {
            Items = new Dictionary<object, object>();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.Attributes.Add("class", "valueSelector");
            HtmlInputText txt = new HtmlInputText();
            txt.Attributes.Add("readOnly", "readOnly");
            txt.Attributes.Add("class", "valueSelectorContent");

            if (!String.IsNullOrEmpty(TooltipText))
                txt.Attributes.Add("title", TooltipText);

            HtmlButton btn = new HtmlButton();
            if (!string.IsNullOrEmpty(OnButtonClick))
            {
                btn.Attributes.Add("onclick", OnButtonClick);
            }
            btn.Attributes.Add("class", "valueSelectorButton ui-state-default ui-corner-all");
            btn.Attributes.Add("type", "button");
            if (!String.IsNullOrEmpty(TooltipText))
                btn.Attributes.Add("title", TooltipText);
            btn.InnerHtml = "...";
            btn.Style.Add(HtmlTextWriterStyle.Width, "25px");

            this.Controls.Add(txt);
            this.Controls.Add(btn);
            //txt.RenderControl(writer);
            //btn.RenderControl(writer);

            base.Render(writer);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.AddJQueryResource(Globals.ResourcePaths.JQueryLibraries.JQueryUI);
            base.AddJQueryResource("JQuery.WebKit.JValueSelector.valueSelector.js");
            base.AddCSSResource("JQuery.WebKit.JValueSelector.valueSelector.css");

            string itemScript = string.Empty;
            foreach (KeyValuePair<object, object> kv in Items)
            {
                itemScript += string.Format("$('#{0}').valueSelector('addItem', '{1}', {2});", this.ClientID, kv.Value, kv.Key);
            }

            //base.AddBodyScript(string.Format("var ctrl = $('#{0}'); ctrl.dropdownchecklist({1}); ctrl.change(function() {{ ctrl.attr('value', ctrl.val()); }});", this.ClientID, this.GetJSProperties("options")));
            base.AddBodyScript(string.Format("$('#{0}').attr('jValue', $.toJSON({1})); $('#{0}').valueSelector({2});", this.ClientID, GetValueAttribute(), this.GetJSProperties("options")));
            //if (!string.IsNullOrEmpty(itemScript))
            //{
            //    base.AddBodyScript(itemScript);
            //}
            base.OnPreRender(e);
        }

        private string GetValueAttribute()
        {
            List<string> values = new List<string>();
            foreach (KeyValuePair<object, object> kv in Items)
            {
                if (kv.Key == null || kv.Value == null)
                {
                    continue;
                }
                values.Add(string.Format("\"{0}\":\"{1}\"", kv.Key, kv.Value));
            }
            return string.Format("{{ {0} }}", string.Join(", ", values));
            //{"802":"Azure Fraud Control Bureau"}
        }
    }
}
